package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.DishService;
import com.sky.service.SetMealService;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author : Sonic
 * @version : 1.0
 * @createTime : 2024-12-04  23:00
 * @description :
 */

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * @param: [shoppingCartDTO]
     * @return: void
     * @description: 加入商品到购物车
     **/
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        // 添加到商品是否存在
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        // 存在只需要数量 +1,update number
        if (list != null && list.size() > 0) {
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartMapper.updateNumberById(cart);
        } else {
            // 不存在就加入,insert
            // 判断是菜品dish还是套餐setmeal
            Long dishId = shoppingCart.getDishId();
            if (dishId != null) {
                // 新增菜品
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            } else {
                // 新增套餐
                Setmeal setmeal = setmealMapper.getById(shoppingCart.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);

        }

    }

    /**
     * @param: []
     * @return: java.util.List<com.sky.entity.ShoppingCart>
     * @description: 根据用户id查询购物车所有数据
     **/
    @Override
    public List<ShoppingCart> showShoppingCart() {
        // 获取当前id
        Long currentId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(currentId);
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        return list;
    }

    /**
     * @param: [shoppingCartDTO]
     * @return: void
     * @description: 删除单个购物车商品,商品数量大于 1就 number--, 等于 1就直接删除表项
     **/
    @Override
    public void delete(ShoppingCartDTO shoppingCartDTO) {
        Long currentId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(currentId);
        // 查询购物车某商品数量是否大于1
        ShoppingCart cart = shoppingCartMapper.list(shoppingCart).get(0);
        // 大于1就number--
        if (cart.getNumber() > 1) {
            cart.setNumber(cart.getNumber()-1);
            shoppingCartMapper.updateNumberById(cart);
        } else {
            // 等于1就直接删除
            shoppingCartMapper.delete(shoppingCart);
        }

    }

    /**
     * @param: []
     * @return: void
     * @description: 根据用户 id清空购物车
     **/
    @Override
    public void cleanShoppingCart() {
        Long currentId = BaseContext.getCurrentId();
        shoppingCartMapper.clean(currentId);
    }
}
